<?php
/**
 * Akimimi front
 * QueryService
 * @author by M.Wang
 * @desc 2009-09-02
 */
require_once '../app/core/services/BaseService.php';
require_once '../app/models/QueryDao.php';
require_once '../app/models/QueryReplyDao.php';

class QueryService extends BaseService {

	/**
	 * QueryDao
	 *
	 * @var QueryDao
	 */
    private $queryDao;

    /**
     * QueryReplyDao
     *
     * @var QueryReplyDao
     */
    private $queryReplyDao;

    public function __construct(){
        parent::__construct();

        $this->queryDao = new QueryDao($this->dbUtil);
        $this->queryReplyDao = new QueryReplyDao( $this->dbUtil );
    }

    public function insertQuery($productId, $queryType, $cid, $status, $content){
        $query = array();
        $query[] = $productId;
        $query[] = $queryType;
        $query[] = $cid;
        $query[] = $status;
        $query[] = $content;
        $queryId = $this->queryDao->saveQuery($query);
        require_once '../app/services/UserPointService.php';
        require_once '../app/constants/UserConstants.php';
        $userPointService = new UserPointService();
        if( strlen($content) > 20 ){
        	$userPoints = $userPointService->getUserTodayPoints( $cid, 2 );
	        if( $userPoints < 200 ){
	            $userPointService->updatePoint( $cid, 0, UserConstants::getAddUserPointCause(2) );
	        }
        }
        
        return $queryId;
    }

    /**
     * Get querys by user_id
     *
     * @param int $int_userid
     * @param int $int_page
     * @param int $int_limit
     * @return array
     */
    public function getQuerysByUserId( $int_userid, $int_page, $int_limit ){
    	$arr_querys = $this->queryDao->getQueryInfoByUserId( $int_userid, $int_page, $int_limit );
    	if( $arr_querys ){
    	   $arr_querys['Count'] = $this->dbUtil->getLastFoundRows();
    	   $arr_querys['Count'] = $arr_querys['Count'][0]['ROWS'];
    	}
    	return $arr_querys;
    }

    /**
     * Get queries data by products.id
     *
     * @param int $int_productid
     * @param int $int_limit
     * @param int $int_pagesize
     * @return array
     */
    public function getQueryByProductId( $int_productid, $int_limit, $int_pagesize, $int_querytype = null, $bool_onlyCount = false ){
        return $this->queryDao->getQueryInfoByProductId( $int_productid, $int_limit, $int_pagesize, $int_querytype, $bool_onlyCount );
    }

    /**
     * Get rows number when you get queries data by products.id
     *
     * @param int $int_productid
     * @param int $int_querytype
     * @return int
     */
    public function getQueryNumberByProductId($int_productid, $int_querytype = null){
        $int_rows = $this->queryDao->getQueryInfoByProductId( $int_productid, null, null, null, true);
        return $int_rows[0]['ROWS'];
    }

    /**
     * Get query data by id
     *
     * @param int $int_queryid
     * @return array
     */
    public function getQueryById( $int_queryid ){
    	$arr_results = $this->queryDao->getQueryInfoById( $int_queryid );
    	if( !empty( $arr_results ) )
    	   return $arr_results[0];
    	return false;
    }

    /**
     * Get reply data by query_id
     *
     * @param int $int_queryid
     * @return array
     */
    public function getReplyByQueryId( $int_queryid ){
    	$arr_results = $this->queryReplyDao->getReplyInfoByQueryId( $int_queryid );
    	if( !empty( $arr_results ) )
    	   return $arr_results[0];
    	return false;
    }

}
?>